package com.sanqing.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sanqing.bean.Message;
import com.sanqing.dao.MessageDAO;
import com.sanqing.factory.MessageDAOFactory;
import com.sanqing.util.Page;
import com.sanqing.util.PageUtil;

/**
 * Servlet implementation class GetMessageList
 */
@WebServlet("/GetMessageList")
public class GetMessageList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetMessageList() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//设置编码
		request.setCharacterEncoding("utf-8");
		
		
		int currentPage=0;
		
		//获取msgList.jsp页面的currentPage
		String currentPageStr = request.getParameter("currentPage");
		//System.out.print(currentPageStr);
		
		if(currentPageStr==null||"".equals(currentPageStr))
		{
			currentPage=1;
		}else{
			currentPage=Integer.parseInt(currentPageStr);
		}
		
		
		MessageDAO messageDAO=MessageDAOFactory.getMessageDAOInstance();//创建message数据库操作对象
		
		Page page=PageUtil.createPage(5, messageDAO.findAllCount(), currentPage);//创建page对象
		
		List<Message>messages=messageDAO.findAllMessage(page);//创建Message列表存储当前页面所有消息
		
		//System.out.println(messages.get(0).getMessageContent());
		
		
		//将从数据库查询到的数据传到jsp中
		request.setAttribute("messageList", messages);
		request.setAttribute("page", page);
		
		//实现跳转
		ServletContext servletContext=getServletContext();
		RequestDispatcher dispatcher=servletContext.getRequestDispatcher("/View/msgList.jsp");
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
